package zl.io;
import java.io.File;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class IODemo01
{
	public static void main(String[] args)
	{
		File f = new File("C:" + File.separator + "Test.txt");
		OutputStream out = null;
		InputStream in = null;
		if(!f.exists())
		{
			try
			{
				out = new FileOutputStream(f,true);
				String str = "Hello World!!!";
				out.write(str.getBytes());
			}
			catch (FileNotFoundException e)
			{
				e.printStackTrace();
			}
			catch(IOException e)
			{
				e.printStackTrace();
			}
			finally
			{
				if(out != null)
				{
					try
					{
						out.close();
					}
					catch (IOException e)
					{
						e.printStackTrace();
					}				
				}
			}
		}
		else
		{
			try
			{
				in = new FileInputStream(f);
				byte[] b = new byte[1024];
				int i = 0;
				while(true)
				{
					i = in.read(b); 
					if(i != -1)
					{
						System.out.print(new String(b,0,i));
					}
					else
						break;
				}	
			}
			catch (FileNotFoundException e)
			{
				e.printStackTrace();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
			finally
			{
				try
				{
					in.close();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		}
	}
}